#include<iostream>
#include<vector>

using namespace std;

int findCircleNum(vector<vector<int>>& isConnected) {
    int n = isConnected.size(), m = isConnected[0].size();
    vector<int> ufs(n, -1);
    auto findroot = [&](int x)->int {
        while (ufs[x] >= 0)
        {
            x = ufs[x];
        }
        return x;
        };
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (isConnected[i][j] == 1)
            {
                int root1 = findroot(i);
                int root2 = findroot(j);
                if (root1 != root2)
                    ufs[root2] = root1;
            }
    int res = 0;
    for (auto it : ufs)
        if (it == -1)
            res++;
    return res;
}